package org.apache.kylin.engine.streaming.util;

import org.apache.kylin.common.KylinConfig;
import org.apache.kylin.common.util.ILock;
import org.apache.kylin.common.util.LockUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Created by taoliu on 2017/4/5.
 */
public class GapUtils {
    private static final Logger logger = LoggerFactory.getLogger(GapUtils.class);
    private static ILock lock;
    static {
        try {
            lock = LockUtils.getTaskHdfsLock(KylinConfig.getInstanceFromEnv().getFillGapTaskLockRoot());
        } catch (Exception e) {
            logger.warn("get lock err");
        }
    }
    public static boolean gapFilling(String cubeName) throws Exception {
        try {
            return lock.lockExist(cubeName);
        } catch (Exception e) {
            logger.error("gapFilling err",e);
            throw e;
        }
    }

    public static boolean addFillingTaskLock(String cubeName) throws Exception {
        return lock.lock(cubeName);
    }

    public static boolean removeFillingTaskLock(String cubeName) throws Exception{
        return lock.unlock(cubeName);
    }
}
